********************************************************************************
* 06_main_regressions.do
* Main Regression Analyses for JHR Paper
* "Teacher Testing Standards and the New Teacher Pipeline"
* Law, Marks, and Stern
*
* Tables Generated:
*   - Table 4: Composite Enrollments (8 columns)
*   - Table 5: Teacher Preparation Program Graduates (8 columns)
*   - Table 6: Alternative TDI Measures (5 columns x 2 panels)
*   - Table 7: Title II Enrollments and Completers (6 columns x 2 panels)
*   - Table A1: Robustness Tests (5 columns x 2 panels)
*   - Table A2: Event Study Coefficients (enrollment + graduation CSVs)
*
* Inputs:
*   - data/cleaned/enrollment_event_data.xlsx
*   - data/cleaned/graduation_event_data.xlsx
*   - data/raw/composite_treatment/*.xlsx (Table 6)
*   - data/raw/title_ii/title_II_completer_clean.xlsx (Table 7)
*
* Note: Bootstrap reps(1000) seed(12345) cluster(State) throughout
*       NO population weights (final accepted paper)
********************************************************************************

clear all
set more off

display "========================================================"
display "06: Main Regression Analyses"
display "========================================================"

capture mkdir "output"
capture mkdir "output/tables"

* Ensure required packages are installed
foreach pkg in reghdfe ftools outreg2 estout {
    capture which `pkg'
    if _rc {
        display "  Installing `pkg'..."
        ssc install `pkg', replace
    }
}

* ══════════════════════════════════════════════════════════════════════════════
* CONTROL VARIABLE GLOBALS
* ══════════════════════════════════════════════════════════════════════════════

global state_demo "real_income unemployment_rate"
global state_kraft "passevals implementevals eliminate_tenure increase_probationary_period weaken_bargaining eliminate_union_dues won_race_top common_core edtpa"
global uni_controls "test_optional satpct2 actpct2 satvr25_2 satvr75_2 satmt25_2 satmt75_2 actcm25_2 actcm75_2 pell_percent2 pell_amount2 loan_percent2 loan_average2 enrollment_total2"
global all_controls "$state_demo $state_kraft $uni_controls"

* ══════════════════════════════════════════════════════════════════════════════
* LOAD AND PREPARE ENROLLMENT DATA
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "Loading enrollment data..."

import excel "data/cleaned/enrollment_event_data.xlsx", firstrow clear

* Drop excluded states
drop if State == "ND" | State == "TN"

* Fill forward test_index within state for AR, CT, DE missing 2018
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018

* Handle CT after 2017: eliminated Praxis Core requirement
replace test_index = . if State == "CT" & year > 2017

* Handle SC after 2017: lowered scores
replace test_index = -.6576 if State == "SC" & year > 2017

* Fill forward any remaining 2020 missings
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020

* Create selectivity from graduation data
* (enrollment data lacks selective — merge from graduation)
preserve
    import excel "data/cleaned/graduation_event_data.xlsx", firstrow clear
    keep unitid selective
    duplicates drop
    bysort unitid: egen sel_max = max(selective)
    keep unitid sel_max
    duplicates drop
    rename sel_max selective_merge
    tempfile sel_data
    save `sel_data'
restore

merge m:1 unitid using `sel_data', keep(master match) nogenerate
capture confirm variable selective
if _rc {
    gen selective = selective_merge
}
capture drop selective_merge
replace selective = 0 if selective == .

* Create shrinking state indicator
gen shrinking = 0
foreach st in "CT" "LA" "ME" "MS" "NH" "NJ" "PA" "VT" "WI" "WV" {
    replace shrinking = 1 if State == "`st'"
}

* Drop missing test_index (CT 2018 after adjustment)
drop if test_index == .

* Drop statefips — bootstrap idcluster(statefips) needs it as a NEW variable
capture drop statefips

display "  Enrollment data: " _N " obs"
quietly tab unitid
display "  Universities: " r(r)
quietly tab State
display "  States: " r(r)

* Save enrollment data for reuse
tempfile enroll_data
save `enroll_data'

* ══════════════════════════════════════════════════════════════════════════════
* LOAD AND PREPARE GRADUATION DATA
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "Loading graduation data..."

import excel "data/cleaned/graduation_event_data.xlsx", firstrow clear

* Drop excluded states
drop if State == "ND" | State == "TN"

* Fill forward test_index within state
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018 & State == "CT"
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018 & State == "AR"
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018 & State == "DE"

sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020

* Handle CT after 2017
replace test_index = . if State == "CT" & year > 2017

* Handle SC after 2017
replace test_index = -.6576 if State == "SC" & year > 2017

* Create shrinking state indicator
gen shrinking = 0
foreach st in "CT" "LA" "ME" "MS" "NH" "NJ" "PA" "VT" "WI" "WV" {
    replace shrinking = 1 if State == "`st'"
}

* Ensure l_ctotalt exists
capture gen l_ctotalt = log(ctotalt + 1)

* Build state-level lagged test_index
* Need 2008 enrollment test_index for lagging to 2009
preserve
    use `enroll_data', clear
    keep if year == 2008
    keep State year test_index
    duplicates drop State year, force
    tempfile enroll_2008
    save `enroll_2008'
restore

* Extract state-year test_index from graduation data
preserve
    keep State year test_index
    duplicates drop State year, force
    append using `enroll_2008'
    duplicates drop State year, force
    sort State year
    by State: gen test_index_lag = test_index[_n-1]
    keep State year test_index_lag
    drop if test_index_lag == .
    tempfile state_lag
    save `state_lag'
restore

merge m:1 State year using `state_lag', keep(master match) nogenerate

* Drop statefips — bootstrap idcluster(statefips) needs it as a NEW variable
capture drop statefips

* Save full graduation data (for event study — needs all rows)
tempfile grad_data_full
save `grad_data_full'

* TWFE sample: need non-missing test_index AND test_index_lag
drop if test_index == . | test_index_lag == .

display "  Graduation TWFE data: " _N " obs"
quietly tab unitid
display "  Universities: " r(r)

tempfile grad_twfe
save `grad_twfe'

* ══════════════════════════════════════════════════════════════════════════════
* TABLE 4: Composite Enrollments (Total Enrollments)
* DV = l_eftotlt (log fall education enrollments + 1)
* Treatment = test_index (contemporaneous)
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "========================================================"
display "TABLE 4: Composite Enrollments"
display "========================================================"

use `enroll_data', clear
local outfile "output/tables/table_4_enrollments.xls"

* Col (1): All, no controls
display "  Col (1): All, no controls..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("No Controls") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, No, Cluster, State) ///
    title("Table 4: Composite Enrollments")

* Col (2): All, full controls
display "  Col (2): All, full controls..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Full Controls") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (3): Less Selective, full controls
display "  Col (3): Less Selective..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls if selective == 0, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Less Selective") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (4): More Selective, full controls
display "  Col (4): More Selective..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls if selective == 1, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("More Selective") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (5): White enrollments, full controls
display "  Col (5): White..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_efwhitt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("White") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (6): Non-White enrollments, full controls
display "  Col (6): Non-White..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_nonwhite test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Non-White") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (7): Shrinking State, full controls
display "  Col (7): Shrinking State..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls if shrinking == 1, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Shrinking") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (8): Growing State, full controls
display "  Col (8): Growing State..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls if shrinking == 0, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Growing") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

display "  Saved: table_4_enrollments.xls"

* ══════════════════════════════════════════════════════════════════════════════
* TABLE 5: Teacher Preparation Program Graduates
* DV = l_ctotalt (log teacher prep completions + 1)
* Treatment = test_index_lag (1-year state-level lag)
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "========================================================"
display "TABLE 5: Teacher Preparation Graduates"
display "========================================================"

use `grad_twfe', clear
local outfile "output/tables/table_5_graduations.xls"

* Col (1): All, no controls
display "  Col (1): All, no controls..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("No Controls") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, No, Cluster, State) ///
    title("Table 5: Teacher Preparation Graduates")

* Col (2): All, full controls
display "  Col (2): All, full controls..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Full Controls") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (3): Less Selective, full controls
display "  Col (3): Less Selective..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls if selective == 0, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Less Selective") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (4): More Selective, full controls
display "  Col (4): More Selective..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls if selective == 1, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("More Selective") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (5): White graduates, 2012 onward
display "  Col (5): White (2012+)..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_cwhitt test_index_lag $all_controls if year >= 2012, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("White") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (6): Non-White graduates, 2012 onward
display "  Col (6): Non-White (2012+)..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_cnonwhite test_index_lag $all_controls if year >= 2012, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Non-White") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (7): Shrinking State, full controls
display "  Col (7): Shrinking State..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls if shrinking == 1, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Shrinking") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (8): Growing State, full controls
display "  Col (8): Growing State..."
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls if shrinking == 0, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Growing") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

display "  Saved: table_5_graduations.xls"

* ══════════════════════════════════════════════════════════════════════════════
* TABLE A1: Robustness Tests Using Alternative Samples
* Panel A = Enrollments (l_eftotlt, test_index)
* Panel B = Graduations (l_ctotalt, test_index_lag)
* All columns use full controls
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "========================================================"
display "TABLE A1: Robustness Tests"
display "========================================================"

* --- Panel A: Enrollments ---
local outfile "output/tables/table_A1_enrollments.xls"

* Col (1): Main sample (same as Table 4 col 2)
display "  Panel A Col (1): Main sample..."
use `enroll_data', clear
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Main") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State) ///
    title("Table A1 Panel A: Enrollment Robustness")

* Col (2): Include ND
display "  Panel A Col (2): Include ND..."
import excel "data/cleaned/enrollment_event_data.xlsx", firstrow clear
drop if State == "TN"
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018
replace test_index = . if State == "CT" & year > 2017
replace test_index = -.6576 if State == "SC" & year > 2017
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
drop if test_index == .
capture drop statefips
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Include ND") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (3): Include TN
display "  Panel A Col (3): Include TN..."
import excel "data/cleaned/enrollment_event_data.xlsx", firstrow clear
drop if State == "ND"
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018
replace test_index = . if State == "CT" & year > 2017
replace test_index = -.6576 if State == "SC" & year > 2017
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
drop if test_index == .
capture drop statefips
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Include TN") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (4): Exclude LA
display "  Panel A Col (4): Exclude LA..."
use `enroll_data', clear
drop if State == "LA"
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Exclude LA") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (5): Drop ME, SC, PA in year >= 2018
display "  Panel A Col (5): Fix ME, SC, PA..."
use `enroll_data', clear
drop if inlist(State, "ME", "SC", "PA") & year >= 2018
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Fix ME,SC,PA") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

display "  Saved: table_A1_enrollments.xls"

* --- Panel B: Graduations ---
local outfile "output/tables/table_A1_graduations.xls"

* Col (1): Main sample (same as Table 5 col 2)
display "  Panel B Col (1): Main sample..."
use `grad_twfe', clear
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Main") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State) ///
    title("Table A1 Panel B: Graduation Robustness")

* Col (2): Include ND
display "  Panel B Col (2): Include ND..."
import excel "data/cleaned/graduation_event_data.xlsx", firstrow clear
drop if State == "TN"
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
replace test_index = . if State == "CT" & year > 2017
replace test_index = -.6576 if State == "SC" & year > 2017
capture gen l_ctotalt = log(ctotalt + 1)
* Build state-level lag including 2008 from enrollment
preserve
    import excel "data/cleaned/enrollment_event_data.xlsx", firstrow clear
    drop if State == "TN"
    keep if year == 2008
    keep State year test_index
    duplicates drop State year, force
    tempfile e2008_nd
    save `e2008_nd'
restore
preserve
    keep State year test_index
    duplicates drop State year, force
    append using `e2008_nd'
    duplicates drop State year, force
    sort State year
    by State: replace test_index = test_index[_n-1] if test_index == .
    by State: gen ti_lag = test_index[_n-1]
    keep State year ti_lag
    drop if ti_lag == .
    tempfile sl_nd
    save `sl_nd'
restore
merge m:1 State year using `sl_nd', keep(master match) nogenerate
rename ti_lag test_index_lag
drop if test_index == . | test_index_lag == .
capture drop statefips
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Include ND") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (3): Include TN
display "  Panel B Col (3): Include TN..."
import excel "data/cleaned/graduation_event_data.xlsx", firstrow clear
drop if State == "ND"
sort State unitid year
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2018
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
by State: replace test_index = test_index[_n-1] if test_index == . & year == 2020
replace test_index = . if State == "CT" & year > 2017
replace test_index = -.6576 if State == "SC" & year > 2017
capture gen l_ctotalt = log(ctotalt + 1)
preserve
    import excel "data/cleaned/enrollment_event_data.xlsx", firstrow clear
    drop if State == "ND"
    keep if year == 2008
    keep State year test_index
    duplicates drop State year, force
    tempfile e2008_tn
    save `e2008_tn'
restore
preserve
    keep State year test_index
    duplicates drop State year, force
    append using `e2008_tn'
    duplicates drop State year, force
    sort State year
    by State: replace test_index = test_index[_n-1] if test_index == .
    by State: gen ti_lag = test_index[_n-1]
    keep State year ti_lag
    drop if ti_lag == .
    tempfile sl_tn
    save `sl_tn'
restore
merge m:1 State year using `sl_tn', keep(master match) nogenerate
rename ti_lag test_index_lag
drop if test_index == . | test_index_lag == .
capture drop statefips
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Include TN") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (4): Exclude LA
display "  Panel B Col (4): Exclude LA..."
use `grad_twfe', clear
drop if State == "LA"
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Exclude LA") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (5): Drop ME, SC, PA in year >= 2020
display "  Panel B Col (5): Fix ME, SC, PA..."
use `grad_twfe', clear
drop if inlist(State, "ME", "SC", "PA") & year >= 2020
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Fix ME,SC,PA") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

display "  Saved: table_A1_graduations.xls"

* ══════════════════════════════════════════════════════════════════════════════
* TABLE A2 / EVENT STUDY REGRESSIONS (Figures 3 & 4)
* Uses pre-computed year_XXXX columns = DeltaTDI * I(year=t)
* Full controls, clustered at state level
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "========================================================"
display "TABLE A2 / EVENT STUDY REGRESSIONS"
display "========================================================"

* --- Enrollment Event Study (biennial, ref=2012) ---
display "  Enrollment event study..."
use `enroll_data', clear

* year_2012 should already be 0 (reference year)
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt year_2008 year_2010 year_2014 year_2016 year_2018 ///
    $all_controls, absorb(unitid year)

* Extract coefficients and SEs to CSV
matrix coefs = e(b)
matrix V = e(V)
local outcsv "output/tables/composite_enrollments_event_study_total.csv"

* Write header
tempname fh
file open `fh' using "`outcsv'", write replace
file write `fh' "year,estimate,std_error,conf_low,conf_high" _n

* Reference year (2012)
local years_enroll "2008 2010 2012 2014 2016 2018"
local coef_idx = 1
foreach yr of local years_enroll {
    if `yr' == 2012 {
        file write `fh' "2012,0,0,0,0" _n
    }
    else {
        local b = coefs[1, `coef_idx']
        local se = sqrt(V[`coef_idx', `coef_idx'])
        local ci_lo = `b' - 1.96 * `se'
        local ci_hi = `b' + 1.96 * `se'
        file write `fh' "`yr'," %21.15f (`b') "," %21.15f (`se') "," %21.15f (`ci_lo') "," %21.15f (`ci_hi') _n
        local coef_idx = `coef_idx' + 1
    }
}
file close `fh'
display "  Saved: composite_enrollments_event_study_total.csv"

* Also save to outreg2
local outfile "output/tables/table_A2_enrollment_event_study.xls"
estadd ysumm
quietly outreg2 using "`outfile'", replace ///
    keep(year_2008 year_2010 year_2014 year_2016 year_2018) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Enrollment ES") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State) ///
    title("Table A2: Event Study Coefficients")

* --- Graduation Event Study (annual, ref=2012) ---
display "  Graduation event study..."
use `grad_data_full', clear

bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt year_2009 year_2010 year_2011 year_2013 year_2014 ///
    year_2015 year_2016 year_2017 year_2018 year_2019 year_2020 ///
    $all_controls, absorb(unitid year)

* Extract coefficients and SEs to CSV
matrix coefs = e(b)
matrix V = e(V)
local outcsv "output/tables/composite_graduations_event_study_total.csv"

tempname fh
file open `fh' using "`outcsv'", write replace
file write `fh' "year,estimate,std_error,conf_low,conf_high" _n

local years_grad "2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020"
local coef_idx = 1
foreach yr of local years_grad {
    if `yr' == 2012 {
        file write `fh' "2012,0,0,0,0" _n
    }
    else {
        local b = coefs[1, `coef_idx']
        local se = sqrt(V[`coef_idx', `coef_idx'])
        local ci_lo = `b' - 1.96 * `se'
        local ci_hi = `b' + 1.96 * `se'
        file write `fh' "`yr'," %21.15f (`b') "," %21.15f (`se') "," %21.15f (`ci_lo') "," %21.15f (`ci_hi') _n
        local coef_idx = `coef_idx' + 1
    }
}
file close `fh'
display "  Saved: composite_graduations_event_study_total.csv"

* Also save to outreg2
local outfile "output/tables/table_A2_graduation_event_study.xls"
estadd ysumm
quietly outreg2 using "`outfile'", replace ///
    keep(year_2009 year_2010 year_2011 year_2013 year_2014 year_2015 year_2016 year_2017 year_2018 year_2019 year_2020) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Graduation ES") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State) ///
    title("Table A2: Event Study Coefficients")

* ══════════════════════════════════════════════════════════════════════════════
* TABLE 6: Robustness Tests Using Alternative Measures of TDI
* Panel A = Enrollments, Panel B = Graduations
* Cols: (1) Math, (2) Reading, (3) Writing, (4) Binding, (5) ETS Min Scores
* Data: pre-merged subject-specific xlsx from JHR R and R/data/composite treatment/
* Treatment: z_score_composite (cols 1-4), test_index (col 5, non-composite)
* NOTE: NO CT post-2017 exclusion for Table 6 (fill-forward instead)
* NOTE: Graduation lag is university-level (not state-level)
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "========================================================"
display "TABLE 6: Alternative TDI Measures"
display "========================================================"

local t6_data "data/raw/composite_treatment"

* --- Panel A: Enrollments ---
local outfile "output/tables/table_6_enrollments.xls"

* Helper program for Table 6 enrollment data prep
capture program drop prep_t6_enroll
program define prep_t6_enroll
    args treat_var
    * Drop ND and TN
    drop if State == "ND" | State == "TN"
    * Fill forward treatment within unitid (including CT — Table 6 uses fill-forward)
    sort unitid year
    by unitid: replace `treat_var' = `treat_var'[_n-1] if `treat_var' == .
    * NOTE: Table 6 does NOT exclude CT post-2017 (uses fill-forward instead)
    * Drop missing treatment
    drop if `treat_var' == .
    * Drop statefips — bootstrap idcluster(statefips) needs it as a NEW variable
    capture drop statefips
end

* Col (1): Math
display "  Panel A Col (1): Math..."
import excel "`t6_data'/enrollments_event_data_math.xlsx", firstrow clear
prep_t6_enroll z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt z_score_composite $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(z_score_composite) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Math") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State) ///
    title("Table 6 Panel A: Enrollment Alternative TDI")

* Col (2): Reading
display "  Panel A Col (2): Reading..."
import excel "`t6_data'/enrollments_event_data_reading.xlsx", firstrow clear
prep_t6_enroll z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt z_score_composite $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(z_score_composite) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Reading") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (3): Writing
display "  Panel A Col (3): Writing..."
import excel "`t6_data'/enrollments_event_data_writing.xlsx", firstrow clear
prep_t6_enroll z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt z_score_composite $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(z_score_composite) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Writing") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (4): Binding Test
display "  Panel A Col (4): Binding..."
import excel "`t6_data'/enrollments_event_data_binding.xlsx", firstrow clear
prep_t6_enroll z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_eftotlt z_score_composite $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(z_score_composite) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Binding") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (5): ETS Min Scores (non-composite test_index)
display "  Panel A Col (5): ETS Min Scores..."
import excel "`t6_data'/enrollments_event_data.xlsx", firstrow clear
* For col 5, treatment is test_index (non-composite)
capture confirm variable test_index_noncomposite
if _rc == 0 {
    prep_t6_enroll test_index_noncomposite
    bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
        reghdfe l_eftotlt test_index_noncomposite $all_controls, absorb(unitid year)
}
else {
    * Fallback: use test_index from this file
    prep_t6_enroll test_index
    bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
        reghdfe l_eftotlt test_index $all_controls, absorb(unitid year)
}
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index*) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("ETS Min") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

display "  Saved: table_6_enrollments.xls"

* --- Panel B: Graduations ---
local outfile "output/tables/table_6_graduations.xls"

* Helper program for Table 6 graduation data prep (university-level lag)
capture program drop prep_t6_grad
program define prep_t6_grad
    args treat_var
    * Drop ND and TN
    drop if State == "ND" | State == "TN"
    * Regenerate non-white variables
    capture drop l_cwhitt l_cnonwhite cnonwhite
    gen cnonwhite = ctotalt - cwhitt
    gen l_cnonwhite = log(cnonwhite + 1)
    gen l_cwhitt = log(cwhitt + 1)
    capture gen l_ctotalt = log(ctotalt + 1)
    * Fill forward treatment within unitid (including CT — Table 6 uses fill-forward)
    sort unitid year
    by unitid: replace `treat_var' = `treat_var'[_n-1] if `treat_var' == .
    * NOTE: Table 6 does NOT exclude CT post-2017 (uses fill-forward instead)
    * University-level lag
    xtset unitid year
    gen `treat_var'_lag = L.`treat_var'
    replace `treat_var'_lag = `treat_var' if `treat_var'_lag == .
    xtset, clear
    * Drop missing lagged treatment
    drop if `treat_var'_lag == .
    * Drop statefips — bootstrap idcluster(statefips) needs it as a NEW variable
    capture drop statefips
end

* Col (1): Math
display "  Panel B Col (1): Math..."
import excel "`t6_data'/graduation_event_data_math.xlsx", firstrow clear
prep_t6_grad z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt z_score_composite_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(z_score_composite_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Math") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State) ///
    title("Table 6 Panel B: Graduation Alternative TDI")

* Col (2): Reading
display "  Panel B Col (2): Reading..."
import excel "`t6_data'/graduation_event_data_reading.xlsx", firstrow clear
prep_t6_grad z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt z_score_composite_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(z_score_composite_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Reading") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (3): Writing
display "  Panel B Col (3): Writing..."
import excel "`t6_data'/graduation_event_data_writing.xlsx", firstrow clear
prep_t6_grad z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt z_score_composite_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(z_score_composite_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Writing") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (4): Binding Test
display "  Panel B Col (4): Binding..."
import excel "`t6_data'/graduation_event_data_binding.xlsx", firstrow clear
prep_t6_grad z_score_composite
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
    reghdfe l_ctotalt z_score_composite_lag $all_controls, absorb(unitid year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(z_score_composite_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Binding") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

* Col (5): ETS Min Scores (non-composite test_index)
display "  Panel B Col (5): ETS Min Scores..."
import excel "`t6_data'/graduation_event_data.xlsx", firstrow clear
capture confirm variable test_index_noncomposite
if _rc == 0 {
    prep_t6_grad test_index_noncomposite
    bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
        reghdfe l_ctotalt test_index_noncomposite_lag $all_controls, absorb(unitid year)
}
else {
    prep_t6_grad test_index
    bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(statefips): ///
        reghdfe l_ctotalt test_index_lag $all_controls, absorb(unitid year)
}
estadd ysumm
quietly outreg2 using "`outfile'", append keep(*_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("ETS Min") dec(2) pdec(3) ///
    addtext(University FE, Yes, Year FE, Yes, Controls, Yes, Cluster, State)

display "  Saved: table_6_graduations.xls"

* --- Combined Table 6 (matches R output format: table_6_alt_tdi.xls) ---
* Import Panel A (enrollments), tag as Panel A, then append Panel B (graduations)
preserve
    import delimited using "output/tables/table_6_enrollments.xls", clear delimiters(tab) varnames(nonames)
    gen panel = "A"
    gen sort_order = _n
    tempfile panel_a
    save `panel_a'

    import delimited using "output/tables/table_6_graduations.xls", clear delimiters(tab) varnames(nonames)
    gen panel = "B"
    gen sort_order = _n + 1000

    append using `panel_a'
    sort panel sort_order
    drop panel sort_order

    export excel using "output/tables/table_6_alt_tdi.xls", replace
    display "  Saved: table_6_alt_tdi.xls (combined enrollments + graduations)"
restore

* ══════════════════════════════════════════════════════════════════════════════
* TABLE 7: Title II Enrollments and Completers
* Panel A = Enrollments (log totalenrollment + 1), Treatment = test_index
* Panel B = Completers (log completerscurrent + 1), Treatment = test_index_lag
* FE: program + year (NOT university + year)
* Controls: State economic + state policy ONLY (no university controls)
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "========================================================"
display "TABLE 7: Title II Enrollments and Completers"
display "========================================================"

import excel "data/raw/title_ii/title_II_completer_clean.xlsx", firstrow clear
rename ipeds_completion_year year

* Construct log DVs
gen log_totalenrollment = log(totalenrollment + 1)
gen log_whiteenrollment = log(whiteenrollment + 1)
gen log_nonwhite_enrollment = log(totalenrollment - whiteenrollment + 1)

* Log completers (should already exist, but ensure)
capture gen log_completerscurrent = log(completerscurrent + 1)
capture gen log_whitecompleters = log(whitecompleters + 1)
gen nonwhite_completers = completerscurrent - whitecompleters
capture gen log_nonwhite_completers = log(nonwhite_completers + 1)

* shrinking variable
capture confirm variable shrinking_state
if _rc == 0 {
    gen shrinking = shrinking_state
}
else {
    gen shrinking = 0
    foreach st in "CT" "LA" "ME" "MS" "NH" "NJ" "PA" "VT" "WI" "WV" {
        replace shrinking = 1 if State == "`st'"
    }
}

* Encode program_f for reghdfe (imported as string, reghdfe needs numeric)
capture confirm string variable program_f
if _rc == 0 {
    * program_f is string — encode to numeric for reghdfe absorb()
    encode program_f, gen(program_fid)
    drop program_f
    rename program_fid program_f
}

display "  Title II data: " _N " obs"

* Title II controls: state-level only (no university controls)
global t7_controls "$state_demo $state_kraft"

* Save full Title II data
tempfile title2_data
save `title2_data'

* --- Panel A: Enrollments ---
local outfile "output/tables/table_7_enrollments.xls"

* Col (1): All Traditional
display "  Panel A Col (1): All Traditional..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_totalenrollment test_index $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("All") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State) ///
    title("Table 7 Panel A: Title II Enrollments")

* Col (2): Shrinking (Traditional)
display "  Panel A Col (2): Shrinking..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index != . & shrinking == 1
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_totalenrollment test_index $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Shrinking") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (3): Growing (Traditional)
display "  Panel A Col (3): Growing..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index != . & shrinking == 0
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_totalenrollment test_index $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Growing") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (4): White (Traditional)
display "  Panel A Col (4): White..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_whiteenrollment test_index $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("White") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (5): Non-White (Traditional) — log(totalenrollment - whiteenrollment + 1), NO pmax
display "  Panel A Col (5): Non-White..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_nonwhite_enrollment test_index $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Non-White") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (6): Alternative Programs
display "  Panel A Col (6): Alternative..."
use `title2_data', clear
keep if programtype == "Alternative" & test_index != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_totalenrollment test_index $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Alternative") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

display "  Saved: table_7_enrollments.xls"

* --- Panel B: Completers ---
local outfile "output/tables/table_7_completers.xls"

* Col (1): All Traditional
display "  Panel B Col (1): All Traditional..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index_lag != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_completerscurrent test_index_lag $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", replace keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("All") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State) ///
    title("Table 7 Panel B: Title II Completers")

* Col (2): Shrinking (Traditional)
display "  Panel B Col (2): Shrinking..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index_lag != . & shrinking == 1
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_completerscurrent test_index_lag $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Shrinking") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (3): Growing (Traditional)
display "  Panel B Col (3): Growing..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index_lag != . & shrinking == 0
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_completerscurrent test_index_lag $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Growing") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (4): White (Traditional)
display "  Panel B Col (4): White..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index_lag != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_whitecompleters test_index_lag $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("White") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (5): Non-White (Traditional)
display "  Panel B Col (5): Non-White..."
use `title2_data', clear
keep if programtype == "Traditional" & test_index_lag != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_nonwhite_completers test_index_lag $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Non-White") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

* Col (6): Alternative Programs
display "  Panel B Col (6): Alternative..."
use `title2_data', clear
keep if programtype == "Alternative" & test_index_lag != .
bootstrap _b, reps(1000) seed(12345) cluster(State) idcluster(state_id): ///
    reghdfe log_completerscurrent test_index_lag $t7_controls, absorb(program_f year)
estadd ysumm
quietly outreg2 using "`outfile'", append keep(test_index_lag) nocons ///
    addstat(Mean of dependent variable, e(ymean)) nor2 ///
    ctitle("Alternative") dec(2) pdec(3) ///
    addtext(Program FE, Yes, Year FE, Yes, Controls, State Only, Cluster, State)

display "  Saved: table_7_completers.xls"

* ──────────────────────────────────────────────────────────────────────────────
* Summary
* ──────────────────────────────────────────────────────────────────────────────

display ""
display "========================================================"
display "06: Main Regressions Summary"
display "========================================================"
display "Created:"
display "  - Table 4: Composite Enrollments (8 cols)"
display "  - Table 5: Teacher Preparation Graduates (8 cols)"
display "  - Table 6: Alternative TDI Measures (5 cols x 2 panels)"
display "  - Table 7: Title II (6 cols x 2 panels)"
display "  - Table A1: Robustness Tests (5 cols x 2 panels)"
display "  - Table A2: Event Study Coefficients (enrollment + graduation CSVs)"
display ""
display "Bootstrap: reps(1000) seed(12345) cluster(State)"
display "NO population weights (final accepted paper)"
display ""
display "06_main_regressions.do complete."
